package com.ncmmall.api.apis.mall;


import com.ncmmall.api.apis.service.Result;
import com.ncmmall.application.business.coupon.CouponApplication;
import com.ncmmall.domain.business.goods.Goods;
import com.ncmmall.querychannel.QueryPage;
import com.ncmmall.querychannel.QueryPageRequest;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.ncmmall.domain.business.marketing.gift.GiftActivity;
import com.ncmmall.domain.business.marketing.gift.QGiftActivity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 赠品活动
 *
 * @author linxs
 * @da
 */
@RestController
@RequestMapping(value = "/gift")
public class GiftApi {

    private Logger logger = LoggerFactory.getLogger(GiftApi.class);

    @Autowired
    private CouponApplication couponApplication;

    @Autowired
    private QueryChannelService queryChannelService;

    @Value("${store.domain}")
    private String storeDomain;

    /**
     * 赠品适用商品列表
     *
     * @param request
     * @param type
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/scopeGoodses", method = RequestMethod.GET)
    @ResponseBody
    public Result drawList(HttpServletRequest request, @RequestParam("id") long id, @RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "20") int pageSize) {
        Result result = new Result();
        try {

            GiftActivity giftActivity = queryChannelService.findOne(QGiftActivity.giftActivity.id.eq(id), GiftActivity.class);

            QueryPageRequest pageRequest = new QueryPageRequest().setPageNo(pageNo).setPageSize(pageSize);
            Map<String, Object> map = new HashMap<>();


            String hql = "";

            if (giftActivity.getScopeGoodsType() == 1) {
                hql += "select g from Goods g where g.merchant.id = :id and status = 3  ";
                map.put("id", giftActivity.getMerchant().getId());
            }

            if (giftActivity.getScopeGoodsType() == 2) {
                hql += "select g from Goods g, GiftScope s where g.id = s.goods.id and s.gift.id = :gid ";
                map.put("gid", giftActivity.getId());
            }

            if (giftActivity.getScopeGoodsType() == 3) {
                hql += "select g from Goods g, GiftChannel c where g.goodsChannel.parent.parent.id = c.channel.id and c.gift.id = :gid ";
                map.put("gid", giftActivity.getId());
            }

            hql += " order by g.createTime desc ";

            QueryPage<Goods> page = queryChannelService.findPage(hql, pageRequest.setParams(map), Goods.class);

            page.getResult().forEach(goods -> {
                goods.setDefaultImage(storeDomain + "/uploads/" + goods.getDefaultImage());
            });

            return result.ok().inject(page);

        } catch (Exception e) {

            logger.error("[赠品适用商品列表获取数据异常]", e);
            return result.fail().setMsg("网络连接失败");
        }

    }

}
